package net.orange.console.genius;

import net.orange.architecture.process.ProcessFactory;
import net.orange.framework.logic.IDepartmentProcess;
import net.orange.framework.model.Department;

/**
 * Department genius is the delegate of IDepartmentProcess object in system
 * console. it can call the IDepartmentProcess object directly in scripting
 * language directly, but can through the department genius using the
 * "$department" in program.
 */
public class DepartmentGenius {
	/**
	 * The unique instance of department genius in system.
	 */
	private static DepartmentGenius instance = null;

	/**
	 * The internal IDepartmentProcess object
	 */
	private IDepartmentProcess process;

	/**
	 * Disabled the default constructor, it need to get object instance through
	 * the getInstance method.
	 */
	private DepartmentGenius() {
		super();
	}

	/**
	 * The private constructor with IDepartmentProcess object.
	 * 
	 * @param process
	 *            the document process.
	 */
	private DepartmentGenius(IDepartmentProcess process) {
		this.process = process;
	}

	/**
	 * Get the unique department genius instance.
	 * 
	 * @return The the department genius instance.
	 * @throws Exception
	 */
	public static DepartmentGenius getInstance() throws Exception {
		if (instance == null) {
			IDepartmentProcess _process = (IDepartmentProcess) ProcessFactory
					.load(Department.class);
			instance = new DepartmentGenius(_process);
		}
		return instance;
	}

	/**
	 * Retrieve the department according the department code.
	 * 
	 * @param code
	 *            The target department code.
	 * @return The department.
	 * @throws Exception
	 */
	public Department find(String code) throws Exception {
		return (Department) process.find("dept_code", code);
	}
}
